import sys
sys.path.append('.')

from utils import LoadFile
import glob
import numpy
import pylab
#import asa
import scipy.optimize,scipy.stats
import random
import os
import cPickle as pickle

import pyximport
pyximport.install()
import pwFunc
from MLFit import *


if __name__=='__main__':
    while True:        
        if not globals().has_key('files'):
            files=glob.glob('data/*.log')
            FileIndex=0
            ResultsDict={}
        else:FileIndex+=1
        File=files[FileIndex]
        figname=os.path.join('Plots',os.path.basename(File).replace('log','pdf'))
        if os.access(figname,os.R_OK):
            raise IOError
        print File
        data=LoadFile(File)
        sizes=data[:,1]
        WCTimes=data[:,3]
        IOTimes=data[:,5]
        rusage=data[:,6]
        rates=data[:,8]
        MRUTimes=data[:,7]
        ix=numpy.argsort(sizes)
        Sizes    = numpy.array(sizes)[ix]
        WCTimes  = numpy.array(WCTimes)[ix]
        CPUTimes = numpy.array(rusage)[ix]
        IOwait   = numpy.array(IOTimes)[ix]
        Rates    = numpy.array(rates)[ix]
        MRUTimes   = numpy.array(MRUTimes)[ix]
        Unit=0.012
        psize=1
        pieces=10
        x=Sizes

        #y=IOwait/Unit; FigLabel="IOw"
        #y1=WCTimes/Unit;FigLabel1="WTC"
        y=WCTimes/Unit; FigLabel="WTC"
        y1=IOwait/Unit; FigLabel1="IOw"

        tmkdir('Plots')
        S=gS(x,pieces=pieces)
        xinit,xmin,xmax=gparamsS(y,x,S,pieces=pieces)

        popsize=max(Consts.CDefGAPopulationSize,20*pieces)

        params=GAFindParams(x,y,S,xmin,xmax,generations=300,stats=100,popsize=popsize)
        """
        params_tnc,nfeval,rc=scipy.optimize.fmin_tnc(pwFunc.pErlangLikelyhood,
            params,
            rgs=(x,y,S),
            approx_grad=1,
            maxfun=10000,disp=1)
        """
        ResultsDict[File]=(S,params)
        save(ResultsDict,"ResultsDict.pickle")

        X=numpy.linspace(x.min(),x.max(),100*pieces)
        Y=pwFunc.gExpectedFunc(X,S,params)
        Ymode=pwFunc.gModeFunc(X,S,params)
        Ymin=pwFunc.gMinFunc(X,S,params)

        #printParams(params,S,X,Ymin,xmin,xmax,Unit=Unit)
        #printParams(params,S,X,Ymode,xmin,xmax,Unit=Unit)
        n=(len(params)-1)/2
        xEx=(S[:-1]+S[1:])*0.5
        yEx=params[1:n+1]
        xm_rate=1.0/lsSlope(xEx,yEx)    
        Rm=1.0/(1.0/params[0]+1.0/(xm_rate))
        #printParams(params,S,X,Y,xmin,xmax,Unit=Unit,label=FigLabel+":avg")
        printParams(params,S,xEx,yEx,xmin,xmax,Unit=Unit,label=FigLabel+":avg")
        fig=pylab.figure(num=6,figsize=(15,8))
        pylab.ion()
        pylab.clf()
        pylab.grid(True)
        pylab.xlabel('Size[MB]')
        uplim=scipy.stats.scoreatpercentile(y,99.9)
        yl='Time[seek]' if Unit<1 else 'Time[s]'
        pylab.ylabel(yl)
        pylab.plot(x,y,linestyle='None',marker='.',markersize=psize,alpha=0.3,label=("%s"%FigLabel))
        pylab.plot(x,y1,linestyle='None',marker='.',markersize=psize,alpha=0.3,label=FigLabel1)

        pylab.plot(X,Y,'-',label=('GA,Expected:%s'%FigLabel),linewidth=2.0)
        pylab.plot(X,Ymode,'-',label=('GA,Mode:%s'%FigLabel),linewidth=2.0)
        pylab.plot(X,Ymin,'-',label=('GA,Min:%s'%FigLabel),linewidth=2.0)
        #pylab.plot(X,Ymode[0]+X/params[0],'-',label=('GA,R:%s'%FigLabel),linewidth=3.0)
        #pylab.plot(X,Ymode[0]+X/xm_rate,'-',label=('GA,T0:%s'%FigLabel),linewidth=3.0)    
        #pylab.plot(X,Ymode[0]+X/Rm,'-',label=('GA,Rmix:%s'%FigLabel),linewidth=3.0)
        pylab.plot(X,Y[0]+X/Rm,'-',label=('GA,Rmix:%s'%FigLabel),linewidth=3.0)

        #pylab.plot(X,Y_tnc,'.-',label='tnc')
        pylab.ylim((0,uplim))
        pylab.legend()

        pylab.title(DistinctLabel(File,files,sep=' '))
        figname=os.path.join('Plots',os.path.basename(File).replace('log','pdf'))
        pylab.savefig(figname)
        figname=os.path.join('Plots',os.path.basename(File).replace('log','png'))
        pylab.savefig(figname)
